package cn.ithers.blog.controller.admin;

import cn.ithers.blog.annotion.CheckTooFrequentCommit;
import cn.ithers.blog.response.ResponseResult;
import cn.ithers.blog.service.IImageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * @Author: zhanghuan
 * @date: 2020/8/28 14:05
 * @description:
 */

@RestController
@Api(value = "图片", tags = "图片")
@RequestMapping("/admin/image")
public class ImageAdminApi {

    @Autowired
    private IImageService imageService;

    /**
     * 图片上传
     * 1.现在比较常用的：对象存储
     * 2.使用Nginx+fastDFS 处理文件上传 nginx负责文件访问
     *
     * @param file
     * @return
     */

    @CheckTooFrequentCommit
    @PreAuthorize("@permission.admin()")
    @ApiOperation(value = "图片上传")
    @PostMapping()
    public ResponseResult uploadImage(@ApiParam(value = "图片文件", required = true) @RequestParam("file") MultipartFile file) {
        return imageService.uploadImage(file);
    }

    /**
     * 删除图片
     *
     * @param imageId
     * @return
     */
    @PreAuthorize("@permission.admin()")
    @ApiOperation(value = "删除图片")
    @DeleteMapping("/{imageId}")
    public ResponseResult deleteImage(@ApiParam(value = "图片ID", required = true) @PathVariable("imageId") String imageId) {
        return imageService.deleteImageById(imageId);
    }

    /**
     * 获取图片列表
     *
     * @param page
     * @param pageSize
     * @return
     */
    @PreAuthorize("@permission.admin()")
    @ApiOperation(value = "获取图片列表")
    @GetMapping("/list/{page}/{pageSize}")
    public ResponseResult listImages(@ApiParam(value = "当前页码", required = true) @PathVariable("page") int page,
                                     @ApiParam(value = "每页显示条数", required = true) @PathVariable("pageSize") int pageSize) {
        return imageService.imageList(page, pageSize);
    }


}
